package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Spot;
import cn.wolfcode.wolf2w.repository.SpotRepository;
import cn.wolfcode.wolf2w.service.ISpotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SpotServiceImpl implements ISpotService{
    @Autowired
    private SpotRepository spotRepository;

    @Override
    public void save(Spot spot) {
        spot.setId(null);
        spotRepository.save(spot);
    }

    @Override
    public void delete(String id) {
        spotRepository.deleteById(id);
    }

    @Override
    public void update(Spot spot) {
        spotRepository.save(spot);
    }

    @Override
    public Spot get(String id) {

        return spotRepository.findById(id).orElse(null);
    }

    @Override
    public List<Spot> list() {
        return spotRepository.findAll();
    }

    @Override
    public void saveOrUpdate(Spot spot) {

        if (StringUtils.hasLength(spot.getId())){
            this.update(spot);
        }else {
            this.save(spot);
        }
    }


}
